<?php include 'session.php'?>
<?php include 'header.php'?>
<?php $m = $_SESSION['m'];?>
<?php 
	include 'ecstore_v1_config.php';
	include ($site_root . '/insp-modules/misc/config.php');
	
	$conn = mysql_connect($host, $user, $password) or die(mysql_error());
	mysql_select_db("ecstore_nm_db");
?>
<?php
if (isset($_POST['submit'])) {
	if ($m == 1) {
	$cid = $_SESSION['cid'];
	$index = $_POST['ship_method'];
	$resultShippingMethods = $soap->call($session_id, "cart_shipping.list", array($cid));
	$code_selected = $resultShippingMethods[$index]['code'];
	$addShippingMethod = $soap->call($session_id, "cart_shipping.method", array($cid, $code_selected));
	$price_ship = number_format($resultShippingMethods[$index]['price'], 2, '.', ',');
	$_SESSION['shipPrice'] = $price_ship;
	} else {
		// echo print_r($_POST['ship_method']);
		$ship = substr($_POST['ship_method'], 0, 1);
		$sql = "SELECT * FROM shipping WHERE id='$ship'";
		$result = mysql_query($sql, $conn) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		$shipPrice = $row['price'];
		$_SESSION['shipPrice'] = $shipPrice;
	}
	header("location: payment.php");
}
?>

<div id="Main">
    <div id="Content">
		<header class="row">
			<h2>Shipping Method</h2>
		</header>
		
		<div class="row">
<?php
if ($m == 1) {
$cid = $_SESSION['cid'];
$resultShippingMethods = $soap->call($session_id, "cart_shipping.list", array($cid));
$num = sizeof($resultShippingMethods);
//echo print_r( $resultShippingMethods );
echo 		'<form action="" method="post">';
for ($i = 0; $i < $num; $i++) {
$code = $resultShippingMethods[$i]['code'];
$carrier_title = $resultShippingMethods[$i]['carrier_title'];
$method_title = $resultShippingMethods[$i]['method_title'];
$price = number_format($resultShippingMethods[$i]['price'], 2, '.', ',');
$message = $carrier_title . " - " . $method_title . " - $" . $price;
			echo "<input type=\"radio\" name=\"ship_method\" value=\"$i\"/>$message<br/>";
}
} else {
	$sql_ship = "SELECT * FROM shipping";
	$result_ship = mysql_query($sql_ship, $conn) or die(mysql_error());
	
	echo	"<form action='' method='post'>";
	while ($row_ship = mysql_fetch_assoc($result_ship)) {
		$id = $row_ship['id'];
		$method = $row_ship['method'];
		$price = $row_ship['price'];
		echo	"<input type='radio' name='ship_method' value='$id' />$method - $$price<br />";
	}
}
echo <<< HERE
				<br/>
				<ul class="buttons">
					<li style="float:left"><input type="button" value="Back" onclick="javascript:history.go(-1);" class="button-secondary" /></li>
					<li style="float:left; margin-left:10px"><input type="submit" value="Next" name="submit" class="button-secondary" /></li>
				</ul>
				<br/>
			</form>
HERE;
?>
		</div>
	</div>

<?php include 'footer.php'?>